// pages/register/register.js
const loginService = require('../../service/loginService.js');
const utils = require('../../utils/util.js');
// load promise 
require('../../utils/wx-pro.js');
var timer = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 60,
    codeText: '发送验证码',
    smsCode: '',
    phoneNum: '',
    disableButton: false,
    showToast: false,
    toastMsg: ''
  },

  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  phoneNumChange: function(e){
    var that = this
    that.setData({
      phoneNum: e.detail.value
    })
  },
  smsCodeChange: function (e) {
    var that = this
    that.setData({
      smsCode: e.detail.value
    })
  },
  validatemobiledata(mobile) {
    if (mobile.length == 0) {
      this.showToastFuc('请输入正确的手机号')
      return false
    }
    if (mobile.length != 11) {
      this.showToastFuc('请输入正确的手机号')
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/
    if (!myreg.test(mobile)) {
      this.showToastFuc('请输入正确的手机号')
      return false
    }
  },
  sendCode: function () {
    if (this.validatemobiledata(this.data.phoneNum) != false){
      this.setData({
        disableButton: true
      })
      var params = {
        mobile: this.data.phoneNum
      }
      loginService.mobileFastLanding(params).then(data => {
        if (data.code == 200) {
          wx.showToast({
            title: '发送成功',
            icon: 'succes',
            duration: 1000
          })
          timer = setInterval(() => {
            if (this.data.time == 0) {
              clearInterval(timer)
              this.setData({
                time: 60,
                codeText: '发送验证码',
                disableButton: false
              })
            } else {
              this.setData({
                time: this.data.time - 1,
                codeText: '重新发送(' + this.data.time + ')'
              })
            }
          }, 1000)
        } else {
          this.showToastFuc(data.message)
        }
      })
        .catch(() => {
          console.log('error')
        })
    }
  },
  bindPhone(){
    var _this = this
    if (_this.validatemobiledata(_this.data.phoneNum) != false && _this.data.smsCode != '') {
      wx.getUserInfo({
        success: function (res2) {
          var params = {
            mobile: _this.data.phoneNum,
            code: _this.data.smsCode,
            headImgUrl: res2.userInfo.avatarUrl,
            nickName: res2.userInfo.nickName
          }
          loginService.xcxBindMobile(params).then(data => {
            if (data.code == 200) {
              wx.setStorageSync('userInfo', JSON.stringify(data.response))
              wx.setStorageSync('sessionId', data.response.sessionId)
              wx.showToast({
                title: '绑定成功',
                icon: 'succes',
                duration: 1000
              })
              wx.navigateBack({})
            } else {
              this.showToastFuc(data.message)
            }
          })
            .catch(() => {
              console.log('error')
            })
        }
      })
    }
  }
})